package com.homer.gps.track;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.maps.GoogleMap;
import com.homer.gps.track.helper.ImageHelper;
import com.homer.gps.track.helper.UsefullStaff;
import com.homer.gps.track.persist.DatabaseHelper;
import com.homer.gps.track.persist.PlaceModel;
import com.homer.gps.track.persist.Repository;
import com.j256.ormlite.android.apptools.OpenHelperManager;

public class PlaceDetail extends SherlockActivity {
	
	private TextView street, house_number, postal_code, city, note, placeType;
	private ImageView photo;
	private String photoPath;
	private int selected_id;
	private PlaceModel selected_place = null;

	private Repository repository;
	private DatabaseHelper dbHelper = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	
		setContentView(R.layout.place_detail);
		this.repository = new Repository(getHelper());
		selected_id = getIntent().getIntExtra("id", -1);
		selected_place = repository.getPlaceForId(selected_id);
		
		initActivity();
	}
	
	private void initActivity() {
		getSupportActionBar().setTitle(selected_place.getName().toString());
		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setIcon(UsefullStaff.getPlaceTypeImageResource(selected_place.getType()));
		street = (TextView) findViewById(R.id.street);
		house_number = (TextView) findViewById(R.id.house_no);
		postal_code = (TextView) findViewById(R.id.postal_code);
		city = (TextView) findViewById(R.id.city);
		note = (TextView) findViewById(R.id.place_note);
		photo = (ImageView) findViewById(R.id.picture_holder);
		placeType = (TextView) findViewById(R.id.place_type);
		
		if(selected_id != -1){
			selected_place = repository.getPlaceForId(selected_id);
			getSupportActionBar().setTitle(selected_place.getName().toString());
			street.setText(selected_place.getStreet().toString());
			house_number.setText(selected_place.getHouse_number().toString());
			postal_code.setText(selected_place.getPostal_code().toString());
			city.setText(selected_place.getCity().toString());
			note.setText(selected_place.getNote().toString());
			if (selected_place.getNote().toString().equals("")) {
				View noteLayout = findViewById(R.id.note_table_row);
				noteLayout.setVisibility(View.GONE);
			}
			if (selected_place.getImagePath() != null) {
				photoPath = selected_place.getImagePath();
				Bitmap decodedBitmap = ImageHelper.decodeBitmap(photoPath);
				if (decodedBitmap != null){
					photo.setImageBitmap(decodedBitmap);
				}
			}else {
				View photoLayout = findViewById(R.id.picture_table_row);
				photoLayout.setVisibility(View.GONE);
			}
		}
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("Edit")
            .setIcon(R.drawable.edit)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        return true;
    }
	
	@Override
	public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
			switch (item.getItemId()) {
			case android.R.id.home:
				finish();
				break;
			case 0:
				Intent i = new Intent(this, PlaceEdit.class);
				i.putExtra("id", selected_id);
				startActivity(i);
				break;
			default:
				break;
			}
		return true;

	}
	
	public void onShowOnMapClicked(View view) {
		if (isNetworkAvailable()) {
			Intent intent = new Intent(this, MapActivity.class);
			intent.putExtra("placeId", selected_id);
		    startActivity(intent);
		}else {
			Toast.makeText(getApplicationContext(), R.string.internet_connection_not_available, Toast.LENGTH_LONG).show();
		}
	}
	
	private DatabaseHelper getHelper() {
	    if (dbHelper == null) {
	        dbHelper = (DatabaseHelper)OpenHelperManager.getHelper(this, DatabaseHelper.class);
	    }
	    return dbHelper;
	}
	
	private boolean isNetworkAvailable() {
	    ConnectivityManager connectivityManager 
	          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
	    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
	}
	
	@Override
	protected void onRestart() {
		Intent intent = new Intent(this, this.getClass());
		intent.putExtra("id", selected_id);
		UsefullStaff.restart(this, intent);
		super.onResume();
	}

}
